<?php

namespace app\filemanage\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Cookie;
use think\Session;
use think\Loader;
use think\Validate;
use think\Config;

class Login extends Controller
{   
    /**
     * 登录页面
     */
    public function index()
    {   
        return $this->fetch();
    }   

    /**
     * 登录方法
     */
    public function login()
    {   
        if (Request::instance()->isPost()) {
            $username = input('post.username');
            $password = input('post.password');
            $captcha  = input('post.captcha');
            $autoFlag = input('?post.autoFlag') ? input('post.autoFlag') : 0;

            //基础验证
            $validate = Loader::validate('Login');
            $data['username'] = $username;
            $data['password'] = $password;
            $data['captcha']  = $captcha;
            if (!$validate->scene('login')->check($data)) {
                return $this->error($validate->getError());
            }

            $where['username'] = $username;
            $userInfo = Db::name('user')->where($where)->find();

            if ($userInfo && $userInfo['password'] === md5($password)) {
                Session::set('uid', $userInfo['id']);
                Session::set('username', $userInfo['username']);
                if ($autoFlag == 1) {
                    $data = array(
                            'username' => $username,
                            'password' => md5($password),
                            'time' => time()+3600*24*7,
                            'key' => WEBKEY
                            );
                    Cookie::set('filemanageAutoLoginFlag', autoLoginFlagEncode($data), 3600*24*7);
                }
                return $this->success('登陆成功！',url('filemanage/index/index'));
            } else {
                return $this->error('密码错误！');
            }
        } else {
            return $this->fetch('login');
        }
    }

    /**
     * 注销方法
     */
    public function logout()
    {
        Session::clear();
        Cookie::delete('filemanageAutoLoginFlag');
        return $this->fetch('index');
    }

    /**
     * 注册方法
     */
    public function register()
    {
        if (Request::instance()->isPost()) {
            $username   = input('post.username');
            $password   = input('post.password');
            $repassword = input('post.repassword');
            $captcha    = input('post.captcha');

            $validate = Loader::validate('Login');
            $data['username']   = $username;
            $data['password']   = $password;
            $data['repassword'] = $repassword;
            $data['captcha']    = $captcha;
            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }

            $value['username'] = $username;
            $value['password'] = md5($password);
            $value['regTime']  = time();

            $id = Db::name('user')->insertGetId($value);
            if($id) {
                mkdir(FILE_ROOT_PATH . 'file' .  DS .$username, 0777, true);
                return $this->success('注册成功','login/index');
            } else {
                return $this->error('注册失败');
            }  
        } else {
            return $this->fetch();
        }
    }
}